from django.http import JsonResponse
# 导入文件存储的模块
from django.core.files.storage import FileSystemStorage
# 从配置文件导入 STATIC_ROOT
from django.conf import settings
#
# from LLMServer.aiserver.settings import STATIC_ROOT

STATIC_ROOT=settings.STATIC_ROOT
def upload(request):
    # 接收用户上传上来的文件
    file = request.FILES.get('userfile123')
    # 创建一个fs实例对象,文件存储在STATIC_ROOT
    fs = FileSystemStorage(location=STATIC_ROOT)
    # 使用fs保存文件到静态资源目录
    saved_filename = fs.save(file.name, file)
    # 获取文件名称
    print('文件名称：', saved_filename)
    # 读取文件内容
    with open(str(STATIC_ROOT) + '/' + saved_filename, 'r', encoding='utf-8') as f:
        file_content = f.read()

    return JsonResponse(
        {
            'code': 200,
            'message': 'success',
            'content': file_content
        }
    )